define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'withdrawals/index' + location.search,
                    add_url: 'withdrawals/add',
                    edit_url: 'withdrawals/edit',
                    del_url: 'withdrawals/del',
                    multi_url: 'withdrawals/multi',
                    import_url: 'withdrawals/import',
                    table: 'withdrawals',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"pending":__('Pending'),"completed":__('Completed'),"failed":__('Failed')}, formatter: Table.api.formatter.status},
                        {field: 'created_at', title: __('Created_at'), formatter: Table.api.formatter.normal},
                        // {field: 'method', title: __('Withdrawal_method'), searchList: {"bank":__('Bank'),"wechat":__('Wechat'),"alipay":__('Alipay')}, formatter: Table.api.formatter.normal},
                        // {field: 'bank', title: __('Bank'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.normal},
                        // {field: 'bankno', title: __('Bankno'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.normal},

                        {field: 'audit_remark', title: __('Failure_reason'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.normal},


                        {
                            field: 'operate',
                            title: __('Operate'),
                            align: 'center',
                            formatter: function (value, row, index) {
                                var html = '';

                                if (row.status == 'pending') {
                                    html += '<a href="javascript:;" class="btn btn-success btn-audit btn-sm" title="待审核">待审核</a>'; // 使用成功绿色
                                } else if (row.status == 'completed') {
                                    html += '<span class="text-secondary" title="已审核">已审核</span>'; // 使用普通灰色
                                } else if (row.status == 'failed') {
                                    html += '<span class="text-danger" title="已驳回">已驳回</span>'; // 使用危险红色
                                }

                                return html;
                            },
                            events: {
                                'click .btn-audit': function (e, value, row, index) {
                                    // 打开审核模态框
                                    $('#batchAuditModal').modal('show');

                                    // 设置当前审核的行ID或者其他需要的信息
                                    $('#confirmAudit').off('click').on('click', function() {
                                        var auditResult = $('input[name="auditResult"]:checked').val();
                                        var rejectReason = $('#rejectReason').val();
                                        var id = row.id; // 当前行的ID

                                        if (auditResult === 'reject' && rejectReason.trim() === '') {
                                            Layer.alert('拒绝理由不能为空！');
                                            return;
                                        }

                                        // 调用审核方法，处理单个审核
                                        Controller.api.review(id, 'single', auditResult, rejectReason);
                                        $('#batchAuditModal').modal('hide'); // 关闭模态框
                                    });

                                    // 处理选择的审核结果，显示或隐藏拒绝理由输入框
                                    $('input[name="auditResult"]').off('change').on('change', function() {
                                        if ($(this).val() === 'reject') {
                                            $('#rejectReasonGroup').show();
                                        } else {
                                            $('#rejectReasonGroup').hide();
                                            $('#rejectReason').val(''); // 清空拒绝理由
                                        }
                                    });

                                    // 初始状态设置
                                    $('#rejectReason').val(''); // 清空拒绝理由
                                    $('#rejectReasonGroup').hide(); // 隐藏拒绝理由输入框
                                },
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            review: function(ids, type, result, reason) {
                // 发送 AJAX 请求示例
                $.ajax({
                    url: 'withdrawals/audit', //
                    type: 'POST',
                    data: {
                        ids: ids,
                        status: result,
                        reason: reason
                    },
                    success: function(response) {
                        Layer.msg('审核成功！');
                        location.reload(); // 刷新当前页面
                        $('#batchAuditModal').modal('hide');
                    },
                    error: function() {
                        Layer.alert('审核失败，请重试！');
                    }
                });
            }
        }
    };
    return Controller;
});
